//
//  PublicViewController.m
//  pocketNews
//
//  Created by DS on 2018/12/13.
//  Copyright © 2018年 徐双. All rights reserved.
//

#import "PublicViewController.h"
#import "APMyFeedTableViewCell.h"
#import "ATRefreshHeader.h"
#import "APMyFeed.h"
#import "APMyFeedContentImage.h"
//通过此方法计算行高
#import "UITableViewCell+ATCalcCellHeight.h"

#import "APAddFeedViewController.h"

#import "BrowPhotoView.h"

@interface PublicViewController ()<UITableViewDelegate,UITableViewDataSource>

Strong UITableView * tableView;

@property (nonatomic, strong) NSMutableArray *feeds;

@end

@implementation PublicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"发现发布";
    
    UIButton *btn1 =[[UIButton alloc]initWithFrame:CGRectMake(0,0, 22, 22)];
    
    [btn1 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn1 setBackgroundImage:[[UIImage imageNamed:@"fanhui"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:btn1];
    self.navigationItem.leftBarButtonItem  = item;
    
    UIButton *btn2 =[[UIButton alloc]initWithFrame:CGRectMake(-22,0, 22, 22)];
    
    [btn2 addTarget:self action:@selector(rightBarButtonItemClick) forControlEvents:UIControlEventTouchUpInside];
    [btn2 setTitle:@"发布" forState:UIControlStateNormal];
    [btn2 setTitleColor:getColor(@"111111") forState:UIControlStateNormal];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithCustomView:btn2];
    self.navigationItem.rightBarButtonItem  = item1;
    
  
    
    
    
    [self.view addSubview:self.tableView];
    
    self.tableView.mj_header = [ATRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
    [self.tableView.mj_header beginRefreshing];
    
    
}

-(void)onClick:(UIButton *)btn{
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)requestData {
    self.feeds = [NSMutableArray array];
    for (int i = 0; i < 13; i++) {
        APMyFeed *myFeed = [[APMyFeed alloc] init];
        myFeed.headIcon = @"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2118739199,3378602431&fm=26&gp=0.jpg";
        myFeed.name = @"王者小能手";
        myFeed.sex = @"性别女";
        if (i % 3 == 0) {
            myFeed.content = @"国服在线接单！今日特价！#王者高校争霸赛#";
            APMyFeedContentImage *image1 = [[APMyFeedContentImage alloc] init];
            image1.imageUrlStr = @"https://ss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=381f7e282b9759ee555066cb82fa434e/0dd7912397dda1449dd17697bfb7d0a20cf4863e.jpg";
            image1.videoUrlStr = @"https://bpic.588ku.com/video_listen/588ku_preview/18/02/09/13/46/03/84/video5a7d359bc470b.mp4";
            myFeed.contentImages = [NSMutableArray arrayWithObjects:image1, nil];
        }
        if (i % 3 == 1) {
            myFeed.content = @"来来来！一起上车！#王者高校争霸赛#";
            APMyFeedContentImage *image1 = [[APMyFeedContentImage alloc] init];
            image1.imageUrlStr = @"https://ss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=381f7e282b9759ee555066cb82fa434e/0dd7912397dda1449dd17697bfb7d0a20cf4863e.jpg";
            image1.videoUrlStr = @"https://bpic.588ku.com/video_listen/588ku_preview/18/02/09/13/46/03/84/video5a7d359bc470b.mp4";
            APMyFeedContentImage *image2 = [[APMyFeedContentImage alloc] init];
            image2.imageUrlStr = @"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2118739199,3378602431&fm=26&gp=0.jpg";
            APMyFeedContentImage *image3 = [[APMyFeedContentImage alloc] init];
            image3.imageUrlStr = @"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2118739199,3378602431&fm=26&gp=0.jpg";
            APMyFeedContentImage *image4 = [[APMyFeedContentImage alloc] init];
            image4.imageUrlStr = @"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2118739199,3378602431&fm=26&gp=0.jpg";
            myFeed.contentImages = [NSMutableArray arrayWithObjects:image1,image2,image3,image4, nil];
        }
        if (i % 3 == 2) {
            myFeed.content = @"关联对象被存储在什么地方，是不是存放在被关联对象本身的内存中？关联对象的五种关联策略有什么区别，有什么坑？关联对象的生命周期是怎样的，什么时候被释放，什么时候被移除？关联对象被存储在什么地方，是不是存放在被关联对象本身的内存中？关联对象的五种关联策略有什么区别，有什么坑？关联对象的生命周期是怎样的，什么时候被释放，什么时候被移除？关联对象被存储在什么地方，是不是存放在被关联对象本身的内存中？关联对象的五种关联策略有什么区别，有什么坑？关联对象被存储在什么地方，是不是存放在被关联对象本身的内存中？关联对象的五种关联策略有什么区别，有什么坑？关联对象的生命周期是怎样的，什么时候被释放，什么时候被移除？关联对象被存储在什么地方，是不是存放在被关联对象本身的内存中？关联对象的五种关联策略有什么区别，有什么坑？关联对象的生命周期是怎样的，什么时候被释放，什么时候被移除？关联对象被存储在什么地方，是不是存放在被关联对象本身的内存中？关联对象的五种关联策略有什么区别，有什么坑？关联对象被存储在什么地方，是不是存放在被关联对象本身的内存中？关联对象的五种关联策略有什么区别，有什么坑？关联对象的生命周期是怎样的，什么时候被释放，什么时候被移除？关联对象被存储在什么地方，是不是存放在被关联对象本身的内存中？关联对象的五种关联策略有什么区别，有什么坑？关联对象的生命周期是怎样的，什么时候被释放，什么时候被移除？关联对象被存储在什么地方，是不是存放在被关联对象本身的内存中？关联对象的五种关联策略有什么区别，有什么坑？";
            APMyFeedContentImage *image1 = [[APMyFeedContentImage alloc] init];
            image1.imageUrlStr = @"https://ss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=381f7e282b9759ee555066cb82fa434e/0dd7912397dda1449dd17697bfb7d0a20cf4863e.jpg";
            image1.videoUrlStr = @"https://bpic.588ku.com/video_listen/588ku_preview/18/02/09/13/46/03/84/video5a7d359bc470b.mp4";
            APMyFeedContentImage *image2 = [[APMyFeedContentImage alloc] init];
            image2.imageUrlStr = @"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2118739199,3378602431&fm=26&gp=0.jpg";
            APMyFeedContentImage *image3 = [[APMyFeedContentImage alloc] init];
            image3.imageUrlStr = @"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2118739199,3378602431&fm=26&gp=0.jpg";
            APMyFeedContentImage *image4 = [[APMyFeedContentImage alloc] init];
            image4.imageUrlStr = @"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2118739199,3378602431&fm=26&gp=0.jpg";
            APMyFeedContentImage *image5 = [[APMyFeedContentImage alloc] init];
            image5.imageUrlStr = @"https://ss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=381f7e282b9759ee555066cb82fa434e/0dd7912397dda1449dd17697bfb7d0a20cf4863e.jpg";
            image5.videoUrlStr = @"https://bpic.588ku.com/video_listen/588ku_preview/18/02/09/13/46/03/84/video5a7d359bc470b.mp4";
            APMyFeedContentImage *image6 = [[APMyFeedContentImage alloc] init];
            image6.imageUrlStr = @"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2118739199,3378602431&fm=26&gp=0.jpg";
            APMyFeedContentImage *image7 = [[APMyFeedContentImage alloc] init];
            image7.imageUrlStr = @"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2118739199,3378602431&fm=26&gp=0.jpg";
            APMyFeedContentImage *image8 = [[APMyFeedContentImage alloc] init];
            image8.imageUrlStr = @"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2118739199,3378602431&fm=26&gp=0.jpg";
            
            myFeed.contentImages = [NSMutableArray arrayWithObjects:image1,image2,image3,image4,image5,image6,image7,image8, nil];
        }
        
        myFeed.time = @"5分钟前";
        myFeed.location = @"南京·麒麟国际企业研发园";
        myFeed.comment = @"12";
        myFeed.zan = @"10";
        myFeed.expand = NO;
        [self.feeds addObject:myFeed];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    }
}


//tabelView
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = getColor(whiteColor);
    }
    return _tableView;
}
#pragma mark - <UITableViewDelegate , UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
   
    
   return self.feeds.count;
  
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    APMyFeedTableViewCell *cell = [APMyFeedTableViewCell cellWithTableView:tableView];
    
    APMyFeed *myFeed = self.feeds[indexPath.row];
    
    cell.myFeed = myFeed;
    
    cell.ATExpandBlock = ^(BOOL isExpand) {
        
        myFeed.expand = isExpand;
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    };
    
    cell.contentImageClick = ^(NSMutableArray * _Nonnull icons, int i) {
        
        
    };
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    APMyFeed * myFeed = self.feeds[indexPath.row];
    
    myFeed.cacheId = indexPath.row + 1;

    
    NSString * stateKey = nil;
    if (myFeed.isExpand) {
        stateKey = @"expanded";
    }else{
        stateKey = @"unexpanded";
    }
    
    CGFloat height = [APMyFeedTableViewCell cellHeightWithTableView:tableView transModel:^(UITableViewCell *sourceCell) {
        
        APMyFeedTableViewCell *cell = (APMyFeedTableViewCell *)sourceCell;
        // 配置数据
        cell.myFeed = myFeed;
        
    } cache:^NSDictionary *{
        return @{ATCacheUniqueKey: [NSString stringWithFormat:@"%ld", myFeed.cacheId],
                 ATCacheStateKey : stateKey,
                 // 如果设置为YES，若有缓存，则更新缓存，否则直接计算并缓存
                 // 主要是对社交这种有动态评论等不同状态，高度也会不同的情况的处理
                 ATRecalcForStateKey : @(YES) // 标识不用重新更新
                 };
    }];
    
   
    
    return height;
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}


#pragma mark - button
- (void)rightBarButtonItemClick {
    APAddFeedViewController *vc = [[APAddFeedViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


@end
